<?php
/* script is intended to run by cronjob
 * 
 */

set_include_path(get_include_path(). PATH_SEPARATOR. 'inc/');
require_once 'inc/globals.secret.php';
function __autoload($class_name) {
  require_once $class_name.'.class.php';
}

if (!array_key_exists (1,$argv) || in_array ("-h", $argv)) {
  echo "usage:   php ".$_SERVER["PHP_SELF"]." <parameters>\n";
  echo "example: php ".$_SERVER["PHP_SELF"]." -h\n";  
  echo "example: php ".$_SERVER["PHP_SELF"]." --current --historical --splits --nolog\n";
  echo "\n";
  die;
}

if (in_array("--nolog", $argv)) {
  $eod = new ftpEoddata($eoddataparams['user'], $eoddataparams['password']);  
} else {
  $db = new db($dbparams["host"], $dbparams["user"], $dbparams["password"]);
  $db->connect("va_stratlab");
  $eod = new ftpEoddata($eoddataparams['user'], $eoddataparams['password'], $db);
}

// connect to EODDATA
if (!$eod->loginToEoddata()) {
  echo $eod->error;
  die;
} else {
  echo "connection established.\n";
}

// for the current data
if (in_array("--current", $argv)) {
  if (!$eod->downloadAllTxtFiles($eoddatadir)) {
    echo $eod->error;
  } else {
    echo "Download of all text files completed.\n";
  } 
}

// current data into date-directory
if (in_array("--currentIntoDate", $argv)) {
  $dirname=$eoddatadir."/".date("Ymd_Hi"); ;
  mkdir($dirname);
  if (!$eod->downloadAllTxtFiles($dirname)) {
    echo $eod->error;
  } else {
    echo "Download of all text files completed.\n";
  }   
}

// for the historical data
if (in_array("--historical", $argv)) {
  if (!$eod->downloadAllTxtFiles($eoddatadir, "./History")) {
    echo $eod->error;
  } else {
    echo "Download of all history text files completed.\n";
  }  
}

// for splits
if (in_array("--splits", $argv)) {
  if (!$eod->downloadAllTxtFiles($eoddatadir."/splits", "./splits")) {
    echo $eod->error;
  } else {
    echo "Download of all split files completed.\n";
  }
}

// disconnect from EODDATA
if (!$eod->disconnectFromEoddata()) {
  echo "could not disconnect.\n";
  die;
} else {
  echo "successfully disconnected.\n";
}

// disconnect from database
if (!in_array("--nolog", $argv)) {
  $db->disconnect();
}

?>
